package com.wxl.common.util

import android.content.Intent
import android.net.Uri
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

/**
 * create file time : 2021/3/15
 * create user : wxl
 * subscribe :
 */
class LaunchPermission {
    companion object {
        /**
         * 拨打电话（跳转到拨号界面，用户手动点击拨打）
         *
         * @param phoneNum 电话号码
         */
        fun callDialPhone(fragment: Fragment, phoneNum: String) {
            fragment.prepareCall(
                ActivityResultContracts.RequestPermission()
            ) {
                if (it) {
                    val intent = Intent(Intent.ACTION_DIAL)
                    val data = Uri.parse("tel:$phoneNum")
                    intent.data = data
                    fragment.startActivity(intent)
                } else {
                    Toll.toll("未获得相关权限")
                }
            }.launch(android.Manifest.permission.CALL_PHONE)

        }


        /**
         * 拨打电话（跳转到拨号界面，用户手动点击拨打）
         *
         * @param phoneNum 电话号码
         */
        fun callDialPhone(context: AppCompatActivity, phoneNum: String) {
            context.prepareCall(
                ActivityResultContracts.RequestPermission()
            ) {
                if (it) {
                    val intent = Intent(Intent.ACTION_DIAL)
                    val data = Uri.parse("tel:$phoneNum")
                    intent.data = data
                    context.startActivity(intent)
                } else {
                    Toll.toll("未获得相关权限")
                }
            }.launch(android.Manifest.permission.CALL_PHONE)

        }

        /**
         * 拨打电话（直接拨打电话）
         * @param phoneNum 电话号码
         */
        fun callPhone(fragment: Fragment, phoneNum: String) {
            fragment.prepareCall(
                ActivityResultContracts.RequestPermission()
            ) {
                if (it) {
                    val intent = Intent(Intent.ACTION_CALL)
                    val data = Uri.parse("tel:$phoneNum")
                    intent.data = data
                    fragment.startActivity(intent)
                } else {
                    Toll.toll("未获得相关权限")
                }
            }.launch(android.Manifest.permission.CALL_PHONE)
        }


        /**
         * 拨打电话（直接拨打电话）
         * @param phoneNum 电话号码
         */
        fun callPhone(context: AppCompatActivity, phoneNum: String) {
            context.prepareCall(
                ActivityResultContracts.RequestPermission()
            ) {
                if (it) {
                    val intent = Intent(Intent.ACTION_CALL)
                    val data = Uri.parse("tel:$phoneNum")
                    intent.data = data
                    context.startActivity(intent)
                } else {
                    Toll.toll("未获得相关权限")
                }
            }.launch(android.Manifest.permission.CALL_PHONE)
        }


        /**
         * 存储权限
         */
        fun getStoragePermission(context: AppCompatActivity, callback: ActivityResultCallback<Boolean>) {
            context.prepareCall(
                ActivityResultContracts.RequestPermission(),callback
            ).launch(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
    }
}